package gsoc.ogsadai.model.rcp.diagram.edit.parts;

import gsoc.ogsadai.model.rcp.diagram.part.RequestVisualIDRegistry;

import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

/**
 * @generated
 */
public class RequestEditPartFactory implements EditPartFactory {

	/**
	 * @generated
	 */
	public EditPart createEditPart(EditPart context, Object model) {
		if (model instanceof View) {
			View view = (View) model;
			switch (RequestVisualIDRegistry.getVisualID(view)) {

			case WorkflowTypeEditPart.VISUAL_ID:
				return new WorkflowTypeEditPart(view);

			case PipelineTypeEditPart.VISUAL_ID:
				return new PipelineTypeEditPart(view);

			case WrappingLabelEditPart.VISUAL_ID:
				return new WrappingLabelEditPart(view);

			case ParallelTypeEditPart.VISUAL_ID:
				return new ParallelTypeEditPart(view);

			case WrappingLabel2EditPart.VISUAL_ID:
				return new WrappingLabel2EditPart(view);

			case SequenceTypeEditPart.VISUAL_ID:
				return new SequenceTypeEditPart(view);

			case WrappingLabel3EditPart.VISUAL_ID:
				return new WrappingLabel3EditPart(view);

			case ActivityTypeEditPart.VISUAL_ID:
				return new ActivityTypeEditPart(view);

			case ActivityTypeInstanceNameEditPart.VISUAL_ID:
				return new ActivityTypeInstanceNameEditPart(view);

			case InputsTypeEditPart.VISUAL_ID:
				return new InputsTypeEditPart(view);

			case InputTypeEditPart.VISUAL_ID:
				return new InputTypeEditPart(view);

			case InputStreamTypeEditPart.VISUAL_ID:
				return new InputStreamTypeEditPart(view);

			case InputStreamTypePipeEditPart.VISUAL_ID:
				return new InputStreamTypePipeEditPart(view);

			case DataTypeEditPart.VISUAL_ID:
				return new DataTypeEditPart(view);

			case OutputsTypeEditPart.VISUAL_ID:
				return new OutputsTypeEditPart(view);

			case OutputStreamTypeEditPart.VISUAL_ID:
				return new OutputStreamTypeEditPart(view);

			case WorkflowType2EditPart.VISUAL_ID:
				return new WorkflowType2EditPart(view);

			case PipelineType2EditPart.VISUAL_ID:
				return new PipelineType2EditPart(view);

			case WrappingLabel4EditPart.VISUAL_ID:
				return new WrappingLabel4EditPart(view);

			case ParallelType2EditPart.VISUAL_ID:
				return new ParallelType2EditPart(view);

			case WrappingLabel5EditPart.VISUAL_ID:
				return new WrappingLabel5EditPart(view);

			case SequenceType2EditPart.VISUAL_ID:
				return new SequenceType2EditPart(view);

			case WrappingLabel6EditPart.VISUAL_ID:
				return new WrappingLabel6EditPart(view);

			case WorkflowType3EditPart.VISUAL_ID:
				return new WorkflowType3EditPart(view);

			case PipelineTypeActivityCompartmentEditPart.VISUAL_ID:
				return new PipelineTypeActivityCompartmentEditPart(view);

			case ParallelTypeWorkflowCompartmentEditPart.VISUAL_ID:
				return new ParallelTypeWorkflowCompartmentEditPart(view);

			case PipelineTypeActivityCompartment2EditPart.VISUAL_ID:
				return new PipelineTypeActivityCompartment2EditPart(view);

			case ParallelTypeWorkflowCompartment2EditPart.VISUAL_ID:
				return new ParallelTypeWorkflowCompartment2EditPart(view);

			case OutputStreamTypePipeEditPart.VISUAL_ID:
				return new OutputStreamTypePipeEditPart(view);
			}
		}
		return createUnrecognizedEditPart(context, model);
	}

	/**
	 * @generated
	 */
	private EditPart createUnrecognizedEditPart(EditPart context, Object model) {
		// Handle creation of unrecognized child node EditParts here
		return null;
	}

	/**
	 * @generated
	 */
	public static CellEditorLocator getTextCellEditorLocator(
			ITextAwareEditPart source) {
		if (source.getFigure() instanceof WrappingLabel)
			return new TextCellEditorLocator((WrappingLabel) source.getFigure());
		else {
			return new LabelCellEditorLocator((Label) source.getFigure());
		}
	}

	/**
	 * @generated
	 */
	static private class TextCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private WrappingLabel wrapLabel;

		/**
		 * @generated
		 */
		public TextCellEditorLocator(WrappingLabel wrapLabel) {
			this.wrapLabel = wrapLabel;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getWrapLabel() {
			return wrapLabel;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getWrapLabel().getTextBounds().getCopy();
			getWrapLabel().translateToAbsolute(rect);
			if (getWrapLabel().isTextWrapOn()
					&& getWrapLabel().getText().length() > 0) {
				rect.setSize(new Dimension(text.computeSize(rect.width,
						SWT.DEFAULT)));
			} else {
				int avr = FigureUtilities.getFontMetrics(text.getFont())
						.getAverageCharWidth();
				rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
						SWT.DEFAULT)).expand(avr * 2, 0));
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}

	/**
	 * @generated
	 */
	private static class LabelCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private Label label;

		/**
		 * @generated
		 */
		public LabelCellEditorLocator(Label label) {
			this.label = label;
		}

		/**
		 * @generated
		 */
		public Label getLabel() {
			return label;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getLabel().getTextBounds().getCopy();
			getLabel().translateToAbsolute(rect);
			int avr = FigureUtilities.getFontMetrics(text.getFont())
					.getAverageCharWidth();
			rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
					SWT.DEFAULT)).expand(avr * 2, 0));
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}
}
